# -*- coding: utf-8 -*-
import gzip
import os

HEADER = \
"""HTTP/1.1 200 OK\r\n"""\
"""Connection: close\r\n"""\
"""Content-Type: text/html\r\n"""\
"""Content-Encoding: gzip\r\n"""\
"""Content-Length: %d\r\n\r\n"""\

def modify(name):
    print "Handling ", name, " ..."
    #Compress data
    f_in = open(name, 'rb')
    f_out = gzip.open(name+'OK', 'wb', 9)
    f_out.writelines(f_in)
    f_out.close()
    f_in.close()
    
    #Read compressed data from file
    f_in = open(name+'OK', 'rb')
    datacomp = f_in.read()
    f_in.close()
    
    #Add header to compressed file
    f_in = open(name+'OK', 'wb')
    f_in.write(HEADER % len(datacomp))
    f_in.write(datacomp)
    f_in.close()
    
    #Summary
    f_in = open(name+'OK', 'rb')
    datacomp = f_in.read()
    f_in.close()
    print "Request length: ", len(datacomp)
    print repr(datacomp[:200])
    print
    
for file in os.listdir(os.getcwd()):
    if "OK" not in file and "." not in file:
        modify(file)
